package com.dragon.loan.service;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dragon.common.entity.search.Searchable;
import com.dragon.common.repository.mybatis.paginator.domain.PageBounds;
import com.dragon.common.service.BaseService;
import com.dragon.loan.entity.CmAttachment;
import com.dragon.loan.repository.CmAttachmentRepository;
import com.marvels.approve.loan.dto.McamAcLoanApplyImageInfo;
import com.marvels.approve.loan.service.IMcamLoanApplyInfoService;

@Service
public class CmAttachmentService extends BaseService<CmAttachment, Long> implements CmAttachmentIDragonService {

	@Autowired
	private CmAttachmentRepository cmAttachmentRepository;
	
	@Autowired
	private IMcamLoanApplyInfoService mcamLoanApplyInfoService;


	@Override
	public List<CmAttachment> queryAttachments(String loanId, String atmtType) {
		return queryAttachments(loanId, new String[] {atmtType});
	}
	
	public List<CmAttachment> queryAttachments(String loanId, String... atmtType) {
		Searchable searchable = new Searchable();
		searchable.addSearchParam("loanId_eq", loanId);
		if (atmtType != null) {
			searchable.addSearchParam("atmtType_in", atmtType);
		}
		//searchable.addSearchParam("status_eq", "A0");
		return selectList(searchable, new PageBounds());
	}


	@Override
	public boolean insertMsg(List<CmAttachment> cmAttachments) {
		try {
			for (CmAttachment cmAttachment : cmAttachments) {
				cmAttachmentRepository.insert(cmAttachment);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public boolean insertMsg(CmAttachment cmAttachment) {
		try {
			cmAttachmentRepository.insert(cmAttachment);
			McamAcLoanApplyImageInfo attachment = new McamAcLoanApplyImageInfo();
			BeanUtils.copyProperties(cmAttachment, attachment);
			mcamLoanApplyInfoService.saveMcamAttachmentInfo(attachment);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
}
